<!--
  * 分账明细
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-col :span="7">
        <a-form-item label="分账日期" class="smart-query-form-item">
          <a-range-picker :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.cTime" @change="onChangeTTime" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="分账订单号" class="smart-query-form-item">
          <a-input v-model:value="queryForm.allocId" placeholder="输入" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="分账明细单号" class="smart-query-form-item">
          <a-input v-model:value="queryForm.id" placeholder="输入" />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="支付通道" class="smart-query-form-item">
          <CustomSelect v-model:value="queryForm.channelId" :placeholder="'请先选择支付通道'" :api="channelApi.queryPage" label-field="channelName" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="分账状态" class="smart-query-form-item">
          <a-select v-model:value="queryForm.status" allowClear :options="ALLOC_DETAIL_STATUS" placeholder="请选择" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="订单号" class="smart-query-form-item">
          <a-input v-model:value="queryForm.orderId" placeholder="输入" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="分账接收方" class="smart-query-form-item">
          <a-input v-model:value="queryForm.receiverAccount" placeholder="输入" />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="所属代理" class="smart-query-form-item">
          <CustomSelect
            v-model:value="queryForm.agentId"
            :placeholder="'请选择所属代理'"
            :api="agentApi.page"
            queryParamKey="companyNameLeftLike"
            label-field="companyName,no"
          />
        </a-form-item>
      </a-col>
      <a-col :span="3">
        <a-form-item label="商户名称" class="smart-query-form-item">
          <CustomSelect
            v-model:value="queryForm.merchantId"
            :placeholder="'请选择'"
            :api="merchantApi.page"
            :queryParams="merchantParams"
            queryParamKey="companyNameLeftLike"
            labelField="companyName,no"
          />
        </a-form-item>
      </a-col>
      <a-col :span="4">
        <a-form-item label="第三方分账订单号" class="smart-query-form-item">
          <a-input v-model:value="queryForm.outAllocId" placeholder="输入" />
        </a-form-item>
      </a-col>
      <!--
      <a-form-item label="状态" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.status" enum-name="STATUS_ENUM" placeholder="状态" />
      </a-form-item>
      -->
      <a-col :span="5">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item></a-col
      >
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <div class="smart-table-setting-block"></div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格begin ----------->
    <a-table
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="id"
      bordered
      :loading="tableLoading"
      :pagination="false"
      :scroll="{ x: 'max-content' }"
    />

    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { allocDetailApi } from '/@/api/business/b-operation/alloc/alloc-detail.js'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import CustomSelect from '/@/components/business/comment/CustomSelect.vue'
  import { channelApi } from '/@/api/business/b-operation/channel/channel-api'
  import { agentApi } from '/@/api/business/b-operation/agent/agent-api'
  import { merchantApi } from '/@/api/business/b-operation/merchant/merchant-api.js'
  import dayjs from 'dayjs'
  // import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { selectDictLabel, formatToMoney } from '/@/utils/common'
  import {
    BILLING_TYPE, //计费类型
    CHARGE_USER_TYPE, //	计费对象
    CHARGE_TYPE, //计费模式
    SETTLE_TYPE, //'结算模式'
    ALLOC_DETAIL_STATUS,
    RECEICER_TYPE, //'分账明细-分账状态'
  } from '/@/constants/dict'
  import { cloneDeep } from 'lodash-es'

  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------
  const columns = ref([
    // {
    //   title: '序号',
    //   dataIndex: 'index',
    //   customRender: ({ index }) => {
    //     return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
    //   },
    // },
    {
      title: '分账订单号',
      dataIndex: 'allocId',
      ellipsis: true,
    },
    {
      title: '第三方分账订单号',
      dataIndex: 'outAllocId',
      ellipsis: true,
    },
    {
      title: '分账明细单号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '订单号',
      dataIndex: 'orderId',
      ellipsis: true,
    },
    {
      title: '订单金额',
      dataIndex: 'orderAmount',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '分账金额',
      dataIndex: 'amount',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'refundAmount',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '实际分账金额',
      dataIndex: 'realityAmount',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '商家账户/商户号',
      dataIndex: 'channelMchid',
      ellipsis: true,
    },
    {
      title: '分账接收方',
      dataIndex: 'receiverAccount',
      ellipsis: true,
    },
    {
      title: '接收方账户类型',
      dataIndex: 'receiverType',
      customRender: ({ text }) => {
        return selectDictLabel(RECEICER_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '分账比例',
      dataIndex: 'ratio',
      customRender: ({ text }) => {
        return `${text}%`
      },
      ellipsis: true,
    },
    {
      title: '分账状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(ALLOC_DETAIL_STATUS, text)
      },
      ellipsis: true,
    },
    {
      title: '所属代理',
      dataIndex: 'oAgentName',
      ellipsis: true,
    },
    {
      title: '商户名称',
      dataIndex: 'merchantName',
      ellipsis: true,
    },
    {
      title: '产品名称',
      dataIndex: 'productName',
      ellipsis: true,
    },
    {
      title: '通道类型',
      dataIndex: 'channelType',
      customRender: ({ text }) => {
        return proxy.$smartEnumPlugin.getDescByValue('ZZB_CHANNEL_TYPE_ENUM', text)
      },
      ellipsis: true,
    },
    {
      title: '分账失败原因',
      dataIndex: 'errorMsg',
      ellipsis: true,
    },
    {
      title: '分账时间',
      dataIndex: 'cTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },

    // {
    //   title: '操作',
    //   dataIndex: 'action',
    //   fixed: 'right',
    //   width: 80,
    // },
  ])
  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    cTime: [dayjs().startOf('day'), dayjs().endOf('day')], //交易时间
    cTimeBegin: dayjs().startOf('day').valueOf(), //交易时间 开始
    cTimeEnd: dayjs().endOf('day').valueOf(), //交易时间 结束
    type: undefined, //计费类型
    settleType: undefined, //结算类型
    chargeType: undefined, //计费模式
    chargeUserType: undefined, //计费用户类型
    allocId: undefined,
    id: undefined,
    channelId: undefined,
    status: undefined,
    orderId: undefined,
    receiverAccount: undefined,
    merchantId: undefined,
    outAllocId: undefined,
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await allocDetailApi.separateAccountDetailsPage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }
  // 时间选择
  function onChangeTTime(dates) {
    queryForm.cTimeBegin = dates[0].startOf('days').valueOf()
    queryForm.cTimeEnd = dates[1].endOf('days').valueOf()
  }

  onMounted(queryData)
</script>
